package com.myzhouye.biz.dto;/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

import com.myzhouye.biz.service.RedisFactoryCacheService;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.*;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @program: demo1
 * @description: 基础工厂
 * @author: 那条蠢鱼
 * @create: 2025-09-29 11:15
 **/
import com.myzhouye.biz.service.PeriodicTaskExecutor;

@Getter
@Setter
@Schema(name = "基础工厂")
public class BaseFactory extends PeriodicTaskExecutor {
    @Schema(description = "工厂id")
    private String id;
    @Schema(description = "工厂名称")
    private String name;
    @Schema(description = "仓库大小")
    private int warehouseSize;
    @Schema(description = "产品名称")
    private String productName;
    @Schema(description = "产品数量")
    private AtomicInteger productNums = new AtomicInteger(0);

    // 添加用户ID字段
    @Schema(description = "所属用户ID")
    private Long userId;

    // 构造函数需要调用父类构造函数
    public BaseFactory(String id, String name, int warehouseSize, String productName, double productionSpeed, Long userId, RedisFactoryCacheService redisFactoryCacheService) {
        super(id, name, productionSpeed, redisFactoryCacheService); // 调用父类构造函数，传入任务ID、名称和周期间隔
        this.id = id;
        this.name = name;
        this.warehouseSize = warehouseSize;
        this.productName = productName;
        this.userId = userId;  // 设置用户ID
    }

    // 实现周期性任务完成时的回调方法
    @Override
    protected void onTaskComplete() {
        if (productNums.get() < warehouseSize) {
            productNums.incrementAndGet();

            // 同步更新Redis中的工厂状态
            getRedisFactoryCacheService().updateFactoryProductCount(getUserId(), getId(), getProductNums())
                    .subscribe();
        }
    }


    public int getProductNums() {
        return productNums.get();
    }

    public void incrementProduct() {
        if (productNums.get() < warehouseSize) {
            productNums.incrementAndGet();
        }
    }

    public void decrementProduct(int count) {
        productNums.addAndGet(-count);
    }
}
